package com.jqweb.controller.Article;

import com.jqweb.controller.Article.pojo.ArticleByCategoryDto;
import com.jqweb.controller.Article.pojo.CreateArticleDto;
import com.jqweb.entity.ArticleEntity;
import com.jqweb.pojo.Result;
import com.jqweb.services.ArticleService;
import com.jqweb.utils.Paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    // 分页获取文章列表
    @PostMapping("/getAll")
    public Result getArticle(@RequestBody Paging paging) {
        Map<String, Object> article = articleService.getAll(paging);
        return Result.success("文章信息获取成功", article);
    }

    // 根据id获取文章详情
    @GetMapping("/getDetail/{id}")
    public Result getArticleDetail(@PathVariable Integer id) {
       ArticleEntity data = articleService.getById(id);
       return Result.success("文章信息获取成功", data);
    }

    // 根据分类获取文章列表
    @PostMapping("/getByCategory")
    public Result getArticleByCategory(@RequestBody ArticleByCategoryDto articleByCategoryDto) {
        System.out.println(articleByCategoryDto);
        Map<String, Object> data = articleService.getByCategory(articleByCategoryDto);
        return Result.success("文章信息获取成功", data);
    }

    // 创建文章
    @PostMapping("/create")
    public Result createArticle(@RequestBody CreateArticleDto createArticleDto) {
        int result = articleService.createArticle(createArticleDto);
        if (result > 0) {
            return Result.success("文章创建成功", null);
        }
        return Result.error("文章创建失败");
    }

    // 修改文章
    @PostMapping("/update")
    public Result updateArticle(@RequestBody ArticleEntity articleEntity) {
        Integer result = articleService.updateArticle(articleEntity);
        if (result > 0) {
            return Result.success("文章修改成功", null);
        }
        return Result.error("文章修改失败");
    }

    // 删除文章
    @GetMapping("/delete/{id}")
    public Result deleteArticle(@PathVariable Integer id) {
        Integer result = articleService.deleteArticle(id);
        if (result > 0) {
            return Result.success("文章删除成功", null);
        }
        return Result.error("文章删除失败");
    }

}
